﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using Antu.Core.DataAccess;

namespace WebSys.UI.Control.ZhengShou
{
    public class FangSanControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack) {
                CustomGridView GridList = (CustomGridView)FindControl("GridList");
                GridList.RowDataBound += new GridViewRowEventHandler(CustomGridList_RowDataBound);
            }
            Bind();
        }
        
        protected void Bind()
        {
            Button search = (Button)FindControl("search");
            Button Submit1 = (Button)FindControl("Submit1");
            Button add = (Button)FindControl("add");
            search.Click += new EventHandler(Go_Search);
            Submit1.Click += new EventHandler(Go_Back);
            add.Click += new EventHandler(Btn_Click);
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            string strpid=HttpContext.Current.Request["pid"];
            List<EluZSProvider.ProLoupan> proloupan = GetProloupanRom("1", strpid, GetSqlWhere());
            GridList.DataSource = proloupan;
            GridList.RecordCount = proloupan.Count;
            GridList.PageSize = 10;
            GridList.DataKeyNames = new string[] { "ID" };
            GridList.DataBind();
            GridList.OnLoadData += new EventHandler(CustomGridList_OnLoadData);
            GridList.PreRender += new EventHandler(CustomGridList_OnPreRnder);
            foreach (GridViewRow gvr in GridList.Rows)
            {
                LinkButton LinkButton1 = (LinkButton)gvr.FindControl("LinkButton1");
                LinkButton1.Click += new EventHandler(Delete_Zhuang_Click);
            }
        }
        protected void CustomGridList_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected void CustomGridList_OnPreRnder(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            string query = HttpContext.Current.Request["query"];
            if (!string.IsNullOrEmpty(query))
            {
                if (!string.IsNullOrEmpty(query))
                {
                    if (query == "loupan")
                    {
                        GridList.Columns[9].Visible = true;
                    }
                    else if (query == "cqzh")
                    {
                        GridList.Columns[10].Visible = true;
                    }
                }
            }
        }


        public List<ProLoupan> GetProloupanRom(string type, string pid, string strwhere)
        {
            List<ProLoupan> proloupanlist = new List<ProLoupan>();
            string sql = @"Select z.*,(Select Zsjdh||Textbox2 From zs_cqzhxy Where fid=z.id ) xyh 
                  From Zs_Proloupan z
                 Where z.楼盘类型 = '" +type+@"'
                   And z.Pid = '" + pid + "' And status = '1'";
            if (!string.IsNullOrEmpty(strwhere))
            {
                sql += strwhere;
            }
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            proloupanlist = DataHelper.DataTableToT<ProLoupan>(dt);
            return proloupanlist;
        }

        protected void CustomGridList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string strID = ((HiddenField)e.Row.FindControl("HiddenField1")).Value;
                bool isview = _EluZSDataProvider.GetRoomXY(strID);
                if (isview)
                {
                    LinkButton lbtn = (LinkButton)e.Row.FindControl("LinkButton1");
                    lbtn.Enabled = false;
                }
            }
        }
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Btn_Click(object sender, EventArgs e)
        {
            string pid = HttpContext.Current.Request["PID"].ToString();
            string url = string.Format("FangSanInfo.aspx?action=insert&query=loupan&id=&PID={0}", pid);
            HttpContext.Current.Response.Redirect(url);
        }
        protected void Go_Back(object sender, EventArgs e)
        {
            string temp = HttpContext.Current.Request["query"];
            if (!string.IsNullOrEmpty(temp))
            {
                if (temp == "cqzh")
                {
                    HttpContext.Current.Response.Redirect("ChanQuanZhiHuanProList.aspx?query=cqzh");
                }
                else
                {
                    HttpContext.Current.Response.Redirect("ZhengShouAnZhiFang.aspx");
                }
            }
            else
            {
                HttpContext.Current.Response.Redirect("ZhengShouAnZhiFang.aspx");
            }
        }
        protected void Delete_Zhuang_Click(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            LinkButton lBtn = sender as LinkButton;
            GridViewRow gvr = lBtn.Parent.Parent as GridViewRow;
            string FID = GridList.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool del = _EluZSDataProvider.DeleteXmByID("zs_proloupan", "ID", FID);
            if (del)
            {
                HttpContext.Current.Response.Redirect("FangSan.aspx?PID=" + HttpContext.Current.Request["PID"] + "&query=loupan");
            }
        }
        protected string GetSqlWhere() {
            string strWhere = string.Empty;
            TextBox dkh = (TextBox)FindControl("dkh");
            TextBox fwdz = (TextBox)FindControl("fwdz");
            if (!string.IsNullOrEmpty(dkh.Text.Trim()))
            {
                strWhere += string.Format(" and 楼层房间号 like '%{0}%'", dkh.Text);
            }
            if (!string.IsNullOrEmpty(fwdz.Text.Trim()))
            {
                strWhere += string.Format(" and 房屋地址 like '%{0}%'", fwdz.Text);
            }
            return strWhere;
        }
    }
}


